package com.zhongjianjian.elasticjob;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.zhongjianjian.elasticjob.utils.FuZaiJiSuan;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Set;

/**
 * 作者: LCG
 * 日期: 2020/2/24 09:50
 * 描述: 定义任务类
 */
@Component
public class MyJobTask implements SimpleJob {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void execute(ShardingContext shardingContext) {

        /*int shardingItem = shardingContext.getShardingItem();//带当前正在执行的分片
        System.out.println("===getShardingItem====>"+shardingItem);
        String jobName = shardingContext.getJobName();//正在执行的JOB的名称
        System.out.println("===jobName====>"+jobName);
        String shardingParameter = shardingContext.getShardingParameter();//当前执行的分片的参数对应  shardingItemParameters
        System.out.println("===shardingParameter====>"+shardingParameter);
        String jobParameter = shardingContext.getJobParameter();//对应设置JobCoreConfiguration的参数jobParameter
        System.out.println("===jobParameter====>"+jobParameter);
        String taskId = shardingContext.getTaskId();//任务的Id
        System.out.println("===taskId====>"+taskId);*/


        String redisKey = FuZaiJiSuan.getRedisKey(redisTemplate, "ZSET", "ZIZENG2", shardingContext.getShardingTotalCount());


        if(shardingContext.getShardingItem()==0){
            System.out.println("============"+shardingContext.getShardingItem());
             chuli(redisKey,shardingContext.getShardingItem());
        }else if(shardingContext.getShardingItem()==1){
            System.out.println("============"+shardingContext.getShardingItem());
             chuli(redisKey,shardingContext.getShardingItem());
        }else if(shardingContext.getShardingItem()==2){
            System.out.println("============"+shardingContext.getShardingItem());
             chuli(redisKey,shardingContext.getShardingItem());
        }else if(shardingContext.getShardingItem()==3){
            System.out.println("============"+shardingContext.getShardingItem());
             chuli(redisKey,shardingContext.getShardingItem());
        }

    }


    public void chuli(String redisKey,int sharding){

        //Set<String> strings = redisTemplate.opsForZSet().rangeByScore(redisKey, 0, new Date().getTime());

        Set<String> strings = redisTemplate.opsForZSet().rangeByScore(redisKey, 0, new Date().getTime(), 0, 2000);

        for (String info:strings) {
            //1处理数据

            //2向MQ发送数据 RabbitMQ
            rabbitTemplate.convertAndSend("TEST-TUISONG-EXCHANGE","TUISONG",info);
            //3从redis删除处理过的数据
            redisTemplate.opsForZSet().remove(redisKey,info);

            System.out.println(sharding+"====处理数据===>剩余的数据==》"+redisTemplate.opsForZSet().zCard(redisKey));

        }
    }

}
